package res.graph;

import java.util.LinkedList;
import java.util.List;

import res.Util;

public class Vertex {
	LinkedList<Vertex> inNeighbours;
	LinkedList<Vertex> outNeighbours;
	private Graph g;
	private int hash = Util.getRandom().nextInt();
	private int ord;
	
	private static int globalOrd = 0;
	private static final Object obj = new Object();
	
	public Vertex(Graph g) {
		inNeighbours = new LinkedList<Vertex>();
		outNeighbours = new LinkedList<Vertex>();
		this.g = g;
		
		synchronized(obj) {
			this.ord = globalOrd++;
		}
	}
	
	public Vertex(Graph g, int ord) {
		inNeighbours = new LinkedList<Vertex>();
		outNeighbours = new LinkedList<Vertex>();
		this.g = g;
		this.ord = ord;
	}
	
	public List<Vertex> getInNeighbourhood() {
		return inNeighbours;
	}
	
	public List<Vertex> getOutNeighbourhood() {
		return inNeighbours;
	}
	
	public Graph getGraph() {
		return g;
	}
	
	public int getOrd() {
		return ord;
	}
	
	public boolean isSource() {
		return !outNeighbours.isEmpty() && inNeighbours.isEmpty();
	}
	
	public boolean isSink() {
		return outNeighbours.isEmpty() && !inNeighbours.isEmpty();
	}
	
	public boolean isIsolated() {
		return outNeighbours.isEmpty() && inNeighbours.isEmpty();
	}
	
	@Override
	public int hashCode() {
		return hash;
	}
	
	@Override
	public boolean equals(Object obj) {
		return (obj instanceof Vertex
				&& ((((Vertex)obj).hash == this.hash && ((Vertex)obj).g == this.g)
					|| ((Vertex)obj).ord == this.ord));
	}
}
